<script lang="ts" setup>
import { reactive, ref, getCurrentInstance } from "vue";
import type {
  FormInstance,
  FormRules,
  UploadProps,
  UploadUserFile,
} from "element-plus";
import { Delete, Download, Plus, ZoomIn } from "@element-plus/icons-vue";
import FuwenbenVue from "./Fuwenben.vue";
import Daohang from "../../components/Daohang.vue"
// import { focusNode } from 'element-plus/es/utils';
import { ElMessage } from 'element-plus'

let { proxy } = getCurrentInstance();
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  name: "",
  desc: "",
  resource: "",
});

const rules = reactive<FormRules>({
  name: [
    { required: true, message: "字数1-50", trigger: "blur" },
    { min: 1, max: 50, message: "字数1-50", trigger: "blur" },
  ],

  desc: [
    { required: true, message: "Please input activity form", trigger: "blur" },
  ],
  resource: [
    {
      required: true,
      message: "请选择新闻类型",
      trigger: "change",
    },
  ],
});

// 上传图片

const dialogImageUrl = ref("");
const dialogVisible = ref(false);
const disabled = ref(false);
const imgarr = [];
var fdata = new FormData();
const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
  //   // 添加图片放数组里面
  if (uploadFiles == undefined) {
    console.log(uploadFile.file, 1111);
    imgarr.push(uploadFile.file);

    let  imgurl = ref(window.URL.createObjectURL(imgarr[0]));
    console.log(imgurl);
    
  } else {
    // 删除图片时将数组对应图片删除
    for (let i = 0; i <= imgarr.length; i++) {
      if (imgarr[i].name == uploadFile.name) {
        imgarr.splice(i, 1);
        console.log(uploadFile, 44, uploadFiles, 55);
        return;
      }
    }
  }
};



const handlePictureCardPreview: UploadProps["onPreview"] = (uploadFile) => {
  console.log(uploadFile.url!);
  
  dialogImageUrl.value = uploadFile.url!;
  dialogVisible.value = true;
};

// 接受子组件的传值

let a = ref("");
const text = (arg: any) => {
  a.value = arg;
  console.log(a.value, 777);
};

// 提交
const submitForm = async (formEl: FormInstance | undefined) => {
  console.log(a.value, 888);
  console.log(formEl);
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid && imgarr.length == 1) {
      fdata.append("title", ruleForm.name);
      fdata.append("type", ruleForm.resource);
      fdata.append("text", a.value);
      if (imgarr.length == 1) {
        console.log(34);
        console.log(imgarr[0], 66);

        fdata.append("preview", imgarr[0]);
      } else {
        return;
      }
      let res = await proxy.$axios.post("/Newsinformation", fdata);
      console.log(res);
    open2()
    } else if (valid && imgarr.length == 0) {
      fdata.append("title", ruleForm.name);
      fdata.append("type", ruleForm.resource);
      fdata.append("text", a.value);
      let res = await proxy.$axios.post("/Newsinformation", fdata);
      console.log(99);
      open2()
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
// 弹窗
const open2 = () => {
  ElMessage({
    message: 'Congrats, this is a success message.',
    type: 'success',
  })
}
// 弹窗
const open1 = () => {
  ElMessage('只能上传一张哦')
}
</script>
<template>
  <Daohang></Daohang>
  <div class="zong">
    <h1>新闻发布</h1>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <!-- 新闻标题 -->
      <el-form-item label="新闻标题" prop="name" class="title">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <!-- 新闻内容 --> 

      <el-form-item label="新闻内容">
        <FuwenbenVue @myclick="text"></FuwenbenVue>
      </el-form-item>



      <!-- 新闻类型二选一 -->
      <el-form-item label="类型" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="公司新闻" />
          <el-radio label="行业动态" />
        </el-radio-group>
      </el-form-item>

      <!-- 上传图片 -->
      <el-form-item
        label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;上传图片 &nbsp;  "
        prop="VehicleCar"
      >
        <el-upload
          :show-file-list="true"
          action=""
          list-type="picture-card"
          :http-request="handleRemove"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :limit="1"
          class="imgchange"
        >

        <el-button :plain="true" @click="open1" class="tanchang"> <el-icon>
            <Plus />
          </el-icon></el-button>

          <!-- <el-icon>
            <Plus />
          </el-icon> -->
        </el-upload>

        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>

      <el-dialog v-model="dialogVisible">
        <img w-full :src="dialogImageUrl" alt="Preview Image" />
      </el-dialog>
      <!-- <FuwenbenVue></FuwenbenVue> -->
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)" :plain="true" >发布</el-button
        >
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
.zong {
  width: 900px;
  margin: 0 auto;

  h1 {
    text-align: center;
    margin-bottom: 20px;
  }
}
</style>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.title {
  width: 827px;
}

/* 弹窗 */
.tanchang{
  border: none;
}

.el-upload{
  background-image:url();
}
</style>
